﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ZX.DBModule;
using ZX.Common;

namespace ZX.BLL.Extend
{
    public static class EntityExtend
    {
        public static int getGuid(this BaseEntity entity)
        {
            Type t = entity.GetType();
            try
            {
                foreach (Attribute attr in t.GetCustomAttributes(true))
                {
                    ZX.DBModule.TableAttribute tableattribute = attr as ZX.DBModule.TableAttribute;
                    if (null != tableattribute)
                    {
                        return (int)Globals.GetPropertyValue(entity, tableattribute.Guid);
                    }
                }
            }
            catch { }
            return 0;
        }

        public static string getKey(this BaseEntity entity)
        {
            if (entity is DataTableBaseEntity)
            {
                //entity
                BaseBussnies bll = new BaseBussnies(entity);
                return bll.GetTableKey(getTableName(entity));
            }
            Type t = entity.GetType();
            foreach (Attribute attr in t.GetCustomAttributes(true))
            {
                ZX.DBModule.TableAttribute tableattribute = attr as ZX.DBModule.TableAttribute;
                if (null != tableattribute)
                {
                    return tableattribute.Guid;
                }
            }
            return "";
        }

        public static string getTableName(this BaseEntity entity)
        {
            if (entity is DataTableBaseEntity)
            {
                return entity.GetTableName();
            }
            Type t = entity.GetType();
            foreach (Attribute attr in t.GetCustomAttributes(true))
            {
                ZX.DBModule.TableAttribute tableattribute = attr as ZX.DBModule.TableAttribute;
                if (null != tableattribute)
                {
                    return tableattribute.TableName;
                }
            }
            return "";
        }


        /// <summary>
        /// 数据保存
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public static bool SaveEntity(this BaseEntity entity)
        {
            CommBLL bllInstance = new CommBLL(entity);
            return bllInstance.Save();
        }

        /// <summary>
        /// 数据删除
        /// </summary>
        /// <param name="entity">实体</param>
        /// <returns></returns>
        public static bool DeleteEntity(this BaseEntity entity)
        {
            CommBLL bllInstance = new CommBLL(entity);
            return bllInstance.Delete();
        }

        public static bool DeleteEntitys(this BaseEntity entity, string Condition)
        {
            CommBLL bllInstance = new CommBLL(entity);
            return bllInstance.Delete(Condition);

        }

        public static Guid? GetKeyFieldValue(this BaseEntity entity)
        {
            if (entity is DataTableBaseEntity)
            {
                return ((DataTableBaseEntity)entity).GetFieldValueList()[entity.getKey()] as Guid?;
            }
            return ZX.Common.Globals.GetPropertyValue(entity, entity.getKey()) as System.Guid?;
        }
        //快速定位特性
        public static void LoadDetailData(this BaseEntity entity)
        {
                
        }

        public static System.Data.DataRow GetEmployeePostDept(this ZX.EHR.Library.DatUser user)
        {
            CommBLL bllInstance = new CommBLL(user);
            System.Data.DataTable table = bllInstance.GetDataBySqlComm(string.Format(@"SELECT     dbo.DatUser.UserID, dbo.DatPost.PostID, dbo.DatPost.PostName, dbo.DatPost.PostLevel, dbo.DatDepartment.LDeptName, 
                      dbo.V_hrEmployee.DeptPath
FROM         dbo.DatPost INNER JOIN
                      dbo.DatDepartment ON dbo.DatPost.DeptID = dbo.DatDepartment.DeptID INNER JOIN
                      dbo.V_hrEmployee ON dbo.DatPost.PostID = dbo.V_hrEmployee.PostID INNER JOIN
                      dbo.DatUser ON dbo.V_hrEmployee.ID = dbo.DatUser.EmpID
where DatPost.PostLevel=0 and UserID='{0}'", user.UserID));
            if (table == null)
                return null;
            if (table.Rows.Count > 0)
            {
                return table.Rows[0];
            }
            else
            {
                return null;
            }
        }

        public static System.Data.DataTable GetUserPostDept(this ZX.EHR.Library.DatUser user)
        {
              CommBLL bllInstance = new CommBLL(user);
            System.Data.DataTable table = bllInstance.GetDataBySqlComm(string.Format(@"select * from (SELECT     dbo.DatUser.UserID, dbo.DatPost.PostID, dbo.DatPost.PostName, dbo.DatPost.PostLevel, dbo.DatDepartment.LDeptName, 
                      dbo.V_hrEmployee.DeptPath
FROM         dbo.DatPost INNER JOIN
                      dbo.DatDepartment ON dbo.DatPost.DeptID = dbo.DatDepartment.DeptID INNER JOIN
                      dbo.V_hrEmployee ON dbo.DatPost.PostID = dbo.V_hrEmployee.PostID INNER JOIN
                      dbo.DatUser ON dbo.V_hrEmployee.ID = dbo.DatUser.EmpID
where DatPost.PostLevel=0 
union
select du.UserId,dp.PostID,dp.PostName,dp.PostLevel,dd.LDeptName,dd.DeptPath from  hrEmployeePositions hp,DatPost dp,hrEmployee he
,DatUser du,DatDepartment dd
where hp.PostType='1'
and hp.PostID=dp.PostID
and hp.EmployeeID=he.ID
and du.EmpID = he.ID
and dd.DeptID =hp.DeptID
and hp.FailDate>GETDATE()
) a 
where UserID='{0}'", user.UserID));
            if (table == null)
                return null;
            if (table.Rows.Count > 0)
            {
                return table;
            }
            else
            {
                return null;
            }

        }
        
    }
}
